<?php
class MainAppController extends AppController {

	var $name = 'Main';
	var $uses = array('Ogloszenia');
	var $permissions = array(
		'dodaj'=>array('admin','manag','users','firms'),
		'edit'=>array('admin','manag','users','firms'),
		'delete'=>array('admin','manag','users','firms'),
		'konto'=>array('admin','manag','users','firms'),
		'statusy'=>array('admin','manag'),
		'statusy_search'=>array('admin','manag')
	);
	var $title = null;
	var $keywords = null;
	var $description = null;

	var $group_id = null;
	

	function beforeRender(){
		parent::beforeRender();
		$this->set('title_for_layout', $this->title);
		$this->set('description_for_layout', $this->description);
		$this->set('keywords_for_layout', $this->keywords);		
	}


	function index() {
		$joins = array();
		$conditions = array('OgloszeniaGroup.group_id'=> $this->group_id,'Ogloszenia.status'=>1);
		$order = array('Ogloszenia.modified' => 'DESC');

		array_push($joins, array('table' => 'ogloszenia_groups', 'alias' => 'OgloszeniaGroup', 'type' => 'INNER', 'conditions' => array( 'OgloszeniaGroup.ogloszenia_id = Ogloszenia.id')));
		
		
		$this->paginate = array('conditions' => $conditions,'limit' => 10,'joins'=>$joins,'order'=>$order);
		$this->Ogloszenia->recursive = 0;
		$ogloszenias = $this->paginate('Ogloszenia');

		$this->set('ogloszenias', $ogloszenias);
	}
	
	function konto() {
		$this->Ogloszenia->recursive = 0;	
		$joins = array();
		array_push($joins, array('table' => 'ogloszenia_groups', 'alias' => 'OgloszeniaGroup', 'type' => 'INNER', 'conditions' => array( 'OgloszeniaGroup.ogloszenia_id = Ogloszenia.id')));
		$this->paginate = array(
			'conditions'=>array('Ogloszenia.user_id'=>$this->Session->read('Auth.User.id'),'OgloszeniaGroup.group_id'=> $this->group_id),
			'limit'=>10,
			'joins'=>$joins);		
		$this->set('ogloszenias', $this->paginate());
	}

	function view($slug = null) {
		if (!$slug) {
			$this->Session->setFlash(__('Nieprawidłowe dane wejciwe!', true));
			$this->redirect(array('action' => 'index'));
		}
		$ogloszenia = $this->Ogloszenia->findBySlug($slug);
		$this->set('ogloszenia',$ogloszenia);
// 		$this->Ogloszenia->query('UPDATE '.$this->Ogloszenia->useTable.' SET viewed  = viewed + 1 WHERE slug = \''.$slug.'\'' );
		$this->title = (is_array($ogloszenia) && isset($ogloszenia['Ogloszenia']['name'])) ? $ogloszenia['Ogloszenia']['name'] : null;		
		$this->description = (is_array($ogloszenia) && isset($ogloszenia['Ogloszenia']['content'])) ? $ogloszenia['Ogloszenia']['content'] : null;
		$this->keywords = (is_array($ogloszenia) && isset($ogloszenia['Ogloszenia']['tags'])) ? $ogloszenia['Ogloszenia']['tags'] : null;
	}

	function dodaj() {
		if (!empty($this->data)) {
			$this->data['Ogloszenia']['user_id'] = $this->Session->read('Auth.User.id');
			$this->data['Ogloszenia']['created'] = date('Y-m-d H:i:s');
			$this->data['Ogloszenia']['modified'] =  date('Y-m-d H:i:s');
			$this->data['Ogloszenia']['slug'] = Inflector::slug($this->data['Ogloszenia']['name'],'-');
			
			$this->Ogloszenia->create();
			if ($this->Ogloszenia->save($this->data)) {
				$this->Session->setFlash(__('Oferta jest zapisana i czeka na zatwierdzenie przez administratora.', true));
				$this->_send(array('to'=>'kowal.sylwester@gmail.com','from'=>$this->Session->read('Auth.User.email'),'subject'=>'dodano wpis na '.$_SERVER['HTTP_HOST']));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('Błd zapisu. proszę ponowić próbę za kilka minut.', true));
			}
		}
		$users = $this->Ogloszenia->User->find('list');
		$groups = $this->Ogloszenia->Group->find('list');
		$this->set(compact('users', 'groups'));
	}
	
	private function _send($params=null){
		$this->Email->to = $params['to'];
		$this->Email->bcc = array('kowal.sylwester@gmail.com');
		$this->Email->subject = $params['subject'];
		$this->Email->from = $_SERVER['HTTP_HOST'].' <'.$params['from'].'>';
		$this->Email->template = 'info-o-wpisie';
		$this->Email->sendAs = 'both'; // text; html; both;
		$this->Email->send();
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ogloszenia', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Ogloszenia']['modified'] =  date('Y-m-d H:i:s');
			$this->data['Ogloszenia']['slug'] = Inflector::slug($this->data['Ogloszenia']['name'],'-');
			
			if ($this->Ogloszenia->save($this->data)) {
				$this->Session->setFlash(__('The ogloszenia has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ogloszenia could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ogloszenia->read(null, $id);
		}
		$users = $this->Ogloszenia->User->find('list');
		$groups = $this->Ogloszenia->Group->find('list');
		$this->set(compact('users', 'groups'));
	}

	function delete($id = null) {
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ogloszenia', true));
			$this->redirect($this->referer());
		}

		$this->data = $this->Ogloszenia->read(null, $id);

		if($this->Session->read('Auth.User.group') == 'admin' || $this->Session->read('Auth.User.group') == 'manag' || $this->Session->read('Auth.User.id') == $this->data['User']['id']  ){
			if ($this->Ogloszenia->delete($id)) {
				$this->Session->setFlash(__('Ogloszenia deleted', true));
				$this->redirect($this->referer());
			}
			$this->Session->setFlash(__('Ogloszenia was not deleted', true));
			$this->redirect($this->referer());
		}else{
			$this->redirect($this->referer());
		}
	}
	
	public function search() {
		$this->Prg->commonProcess();
		$this->paginate['limit'] = 10;
		$this->paginate['conditions'] = $this->Ogloszenia->parseCriteria($this->passedArgs);
		$this->paginate['order'] = array('OgloszeniaGroup.group_id','Ogloszenia.modified' => 'DESC');
		$this->paginate['joins'] = array(array('table' => 'ogloszenia_groups', 'alias' => 'OgloszeniaGroup', 'type' => 'INNER', 'conditions' => array( 'OgloszeniaGroup.ogloszenia_id = Ogloszenia.id')));
		// jesli admin to wszystkie statusy
		if($this->Session->check('Auth.User.id') && $this->Auth->user('group') != 'admin')
		array_push($this->paginate['conditions'], array('Ogloszenia.status'=>1));
		$this->Ogloszenia->recursive = 1;
		$this->set('ogloszenias', $this->paginate());
	}
	
	public function statusy_search() {
		$this->Prg->commonProcess();
		$this->paginate['limit'] = 10;
		$this->paginate['conditions'] = $this->Ogloszenia->parseCriteria($this->passedArgs);
		$this->paginate['order'] = array('OgloszeniaGroup.group_id','Ogloszenia.modified' => 'DESC');
		$this->paginate['joins'] = array(array('table' => 'ogloszenia_groups', 'alias' => 'OgloszeniaGroup', 'type' => 'INNER', 'conditions' => array( 'OgloszeniaGroup.ogloszenia_id = Ogloszenia.id')));
		// jesli admin to wszystkie statusy
		if($this->Session->check('Auth.User.id') && $this->Auth->user('group') != 'admin')
		array_push($this->paginate['conditions'], array('Ogloszenia.status'=>1));
		$this->Ogloszenia->recursive = 1;
		$this->set('ogloszenias', $this->paginate());
	}
	
	function statusy() {
		if (!empty($this->data)) {
			foreach ($this->data['Ogloszenia'] as $r) {
				$this->Ogloszenia->save($r);
			}			
		}
		$joins = array();
		$conditions = array();
		$order = array('Ogloszenia.status' => 'ASC');
	
		array_push($joins, array('table' => 'ogloszenia_groups', 'alias' => 'OgloszeniaGroup', 'type' => 'INNER', 'conditions' => array( 'OgloszeniaGroup.ogloszenia_id = Ogloszenia.id')));
	
		$this->paginate = array('conditions' => $conditions,'limit' => 10,'joins'=>$joins,'order'=>$order);
		$this->Ogloszenia->recursive = 0;
		$ogloszenias = $this->paginate('Ogloszenia');
	
		$this->set('ogloszenias', $ogloszenias);
	}	
}
